package com.bwie.parkdemo.mvvm.viewmodel;

import android.app.Application;
import android.databinding.ObservableField;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.View;

import com.bwie.lib_core.action.ActActionField;
import com.bwie.lib_core.base.BaseEntity;
import com.bwie.lib_core.mvvm.base.BaseActivity;
import com.bwie.lib_core.mvvm.base.BaseViewModel;
import com.bwie.parkdemo.mvvm.model.SelVisitorModel;
import com.bwie.parkdemo.mvvm.model.entity.VisitorEntity;
import com.bwie.parkdemo.mvvm.ui.activity.VisitorValuesActivity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 全部来访列表ViewModel
 * */
public class VisitorViewModel extends BaseViewModel<SelVisitorModel> {

    //根据原型图中全部来访列表样式构建对应数据源
    public ObservableField<List<Object>>datas = new ObservableField<>();//databinding下单向绑定数据源->List<VisitorEntity>
    public int adapterType = 3;//自定义属性adapter type类型
    public VisitorItemListener listener;//recyclerview点击事件监听
    public VisitorChildItemListener childItemListener;//子控件点击事件
    private int userId;//用户id->根据用户id获取当前用户下的全部申请
    private List<Object> list;//原始与列表绑定的数据源
    public VisitorViewModel(@NonNull Application application) {
        super(application);
    }
    @Override
    protected SelVisitorModel createModel() {
        return new SelVisitorModel();
    }
    @Override
    protected void initData() {
        userId = m.requestUserId(getApplication());
//        userId = 48;
        childItemListener = new VisitorChildItemListener();
        listener = new VisitorItemListener();
        list = new ArrayList<>();
        datas.set(list);
        //请求获取当前用户对应的全部申请
        requestVisitor();
    }

    private void requestVisitor(){
        //请求参数封装
        Map<String,Object>map = new HashMap<>();
        map.put("userId",userId);
        String jsonStr = new Gson().toJson(map);
        RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
        m.request(body).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addCompositeDisposable(d);
                        showDialog();
                    }

                    @Override
                    public void onNext(BaseEntity entity) {
                        VisitorEntity visitorEntity = (VisitorEntity) entity;
//                        Log.e("ZXY","VisitorViewModel:"+visitorEntity.getDatas().size());
                        list.clear();
                        list.addAll(visitorEntity.getDatas());
                        datas.notifyChange();
                    }

                    @Override
                    public void onError(Throwable e) {
                        disDialog();
                        Log.e("ZXWQ","请求出错:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        disDialog();
                    }
                });
    }

    //当前viewmodel对应的recyclerview下的itemview单击事件
    public class VisitorItemListener implements BaseQuickAdapter.OnItemClickListener{
        @Override
        public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
            //跳转到预约申请详情页
            //1.打印对应点击事件获取id
            VisitorEntity.Values entity = (VisitorEntity.Values) adapter.getItem(position);
            Log.e("ZXY","VisitorItemListener:"+entity.visitorId);
            //2.界面跳转封装
            Map<String,Object>map = new HashMap<>();
            map.put(ActActionField.ACTCLAZZ, VisitorValuesActivity.class);
            //封装要传递的bundle参数
            Bundle bundle = new Bundle();
            bundle.putString(ActActionField.ACTBUNDLE,entity.visitorId);
            bundle.putString("start",entity.visitorStart);
            bundle.putString("end",entity.visitorEnd);
            bundle.putString("time",entity.visitorTime);
            bundle.putString("carNum",entity.carNum);
            bundle.putString("parkId",entity.parkId);
            map.put(ActActionField.ACTBUNDLE,bundle);
            startAct(map);
        }
    }

    //当前viewmodel对应的recyclerview下的childitemview单击
    public class VisitorChildItemListener implements BaseQuickAdapter.OnItemChildClickListener{

        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            Log.e("ZXWQ","onItemChildClick:"+position);
            VisitorEntity.Values values = (VisitorEntity.Values) adapter.getItem(position);
            Map<String,Object>map = new HashMap<>();
            map.put("visitorId",values.visitorId);
            String jsonStr = new Gson().toJson(map);
            RequestBody body = RequestBody.create(MediaType.parse("application/json"),jsonStr);
            m.requestDelVisitor(body).subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<BaseEntity>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            addCompositeDisposable(d);
                            showDialog();
                        }

                        @Override
                        public void onNext(BaseEntity baseEntity) {
                        }

                        @Override
                        public void onError(Throwable e) {
                            showMsg("清除出错:"+e.getMessage());
                        }

                        @Override
                        public void onComplete() {
                            disDialog();
                            //1.服务器接口返回时有删除数据的id->比较原始数据源中id与删除的数据id相同时
                            //移除数据刷新列表
                            //2.服务器接口返回时没有删除数据的id字段->重新请求列表
                            requestVisitor();
                        }
                    });
        }

    }

}
